import { Table, TablePaginationConfig, TableProps } from 'antd';
import React, { useMemo } from 'react';

import { DataSource } from '@/models/DataSource';
import { Pagination } from '@/models/Pagination';

import './index.less';

interface IProps extends Omit<TableProps<any>, 'pagination' | 'dataSource'> {
  pagination: Pagination | false;
  dataSource?: DataSource<any>;
}

const GTable = (props: IProps) => {
  const { pagination, dataSource, ...otherProps } = props;
  const { records, total, pages } = dataSource || {};

  const pageConfig = useMemo(() => {
    let config: false | TablePaginationConfig = false;
    if (pagination && pages) {
      const { current, pageSize } = pagination;
      config = {
        size: 'default',
        className: 'pagination-box',
        showSizeChanger: true,
        showQuickJumper: true,
        total,
        current,
        pageSize,
        showTotal: (sum: number) => `共${total || sum}条记录  第${current}/${pages}页`,
      };
    }
    return config;
  }, [pages, pagination, total]);

  return <Table {...otherProps} pagination={pageConfig} dataSource={records} />;
};

export default GTable;
